﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using CefSharp.Wpf;
using KeZhou.Models.Instances;
using KeZhou.Models;
using System.Windows.Controls;
using Newtonsoft;
using Newtonsoft.Json.Linq;
using System.Windows;
using KeZhou.Views.Components.Controls;
using Newtonsoft.Json;
using Models.Wishire;
using Models.Wishire;
using Models.Wishire.entity;
using CefSharp.DevTools.FedCm;
using Google.Protobuf.WellKnownTypes;
using System.Windows.Media.Animation;

namespace KeZhou.ViewModels
{
    internal partial class InfoViewModel: ObservableObject
    {
        private static InfoViewModel instance;

        public static InfoViewModel Instance => instance ?? (instance = new InfoViewModel());

        public InfoViewModel()
        {
            CityDataDao cityDao= new CityDataDao();
            foodData = cityDao.getCityDatas(PlanOnBuilding.Instance.cityName, "必吃");
            placeData = cityDao.getCityDatas(PlanOnBuilding.Instance.cityName, "景区");
            mustData = cityDao.getCityDatas(PlanOnBuilding.Instance.cityName, "必逛");

            //TODO@wxy
            //初始化时获取当前账号在当前城市的planList

            //solve interface = new solve();
            solve ans = new solve();
            var plan = ans.find_plan((int)PlanOnBuilding.Instance.accountId,PlanOnBuilding.Instance.cityName);
            List<TmpPlan> planList = new List<TmpPlan>();
            for (int i = 0; i < plan.Count(); i++)
                planList.Add(new TmpPlan
                {
                    planId = plan[i].GetPlanId(),
                    planName = plan[i].GetPlanName(),
                    startTime = plan[i].GetStartTime(),
                    endTime = plan[i].GetEndTime()
                });
            //end
            var json = JsonConvert.SerializeObject(new { planList });
            MainThreeInfo.Instance.InfoBrowser.ExecuteScriptAsyncWhenPageLoaded($"document.initPlanList({json});");
        }

        private ChromiumWebBrowser browser;

        public void setBrowser(ChromiumWebBrowser browser)
        {
            this.browser = browser;
        }

        [ObservableProperty]
        private List<POI> foodData;
        [ObservableProperty]
        private List<POI> placeData;
        [ObservableProperty]
        private List<POI> mustData;

        [ObservableProperty]
        private string searchText;

        [RelayCommand]
        private void Search()
        {
            var jsCode = $"searchAddress('{SearchText}')";
            browser.ExecuteScriptAsync(jsCode);
        }

        [RelayCommand]
        private void POISelected(POI selectedPOI)
        {
            JObject address = new JObject();
            address["name"] = selectedPOI.Name;
            address["type"] = selectedPOI.Type;
            address["lng"] = selectedPOI.Lng;
            address["lat"] = selectedPOI.Lat;
            var jsCode = $"showPOI({address})";
            browser.ExecuteScriptAsync(jsCode);

            //选择兴趣点后，vue展示详情
            var json = JsonConvert.SerializeObject(new { cityName = PlanOnBuilding.Instance.cityName , targetName = selectedPOI.Name });

            MainThreeInfo.Instance.InfoBrowser.ExecuteScriptAsyncWhenPageLoaded($"document.refreshDetail({json});");
        }

        public void setCurrentPOI(string poiName)
        {
            var json = JsonConvert.SerializeObject(new { cityName = PlanOnBuilding.Instance.cityName, targetName = poiName });

            MainThreeInfo.Instance.InfoBrowser.ExecuteScriptAsyncWhenPageLoaded($"document.refreshDetail({json});");
        }

        //TODO@wxy
        //向数据库中新增一个计划，此时软件的生命周期PlanOnBuilding里已经有了accountId、城市、时间、酒店等
        public void setNewPlan(string planName)
        {
            int accountId = (int)PlanOnBuilding.Instance.accountId;
            string cityName=PlanOnBuilding.Instance.cityName;
            // Todo 更改了PlanOnBuilding的startTime
            string startTime = PlanOnBuilding.Instance.startTime;
            string endTime = PlanOnBuilding.Instance.endTime.ToString();
            DateTime setTime = DateTime.Now;
            string empty = "";
            string hotelName = PlanOnBuilding.Instance.hotel;
            double hotelLat = 0;
            hotelLat = PlanOnBuilding.Instance.hotelLnglat[1];
            double hotelLng = 0;
            hotelLng =PlanOnBuilding.Instance.hotelLnglat[0];
            plan_st plan = new plan_st();
            plan.SetAccountId(accountId);
            plan.SetCityName(cityName);
            plan.SetStartTime(startTime);
            plan.SetEndTime(endTime);
            plan.SetSetTime(setTime);
            plan.SetEmpty(empty);
            plan.SetPlanName(planName);
            plan.SethotelName(hotelName);
            plan.SethotelLat(hotelLat);
            plan.SethotelLng(hotelLng);
            solve ans = new solve();
            ans.plan_insert(plan);

            var seplan = ans.find_plan((int)PlanOnBuilding.Instance.accountId, PlanOnBuilding.Instance.cityName);
            List<TmpPlan> planList = new List<TmpPlan>();
            for (int i = 0; i < seplan.Count(); i++)
                planList.Add(new TmpPlan
                {
                    planId = seplan[i].GetPlanId(),
                    planName = seplan[i].GetPlanName(),
                    startTime = seplan[i].GetStartTime(),
                    endTime = seplan[i].GetEndTime()
                });
            //end
            var json = JsonConvert.SerializeObject(new { planList });
            MainThreeInfo.Instance.InfoBrowser.ExecuteScriptAsyncWhenPageLoaded($"document.initPlanList({json});");

        }

        //TODO@wxy
        //添加一个target
        public void setTarget(TmpTarget target)
        {
            /*public int planId { get; set; }
            public string targetName { get; set; }
            public double targetLat { get; set; }
            public double targetLng { get; set; }
            public string targetAddress { get; set; }
            public string targetType { get; set; }*/

             int planId = target.planId;
             string targetCity = PlanOnBuilding.Instance.cityName;
             string targetAddress = target.targetAddress;
             string targetName = target.targetName;
             string targetType = target.targetType;
             double targetLat = target.targetLat;
             double targetLng = target.targetLng;
             string empty;
             int Tag = 1;
            target_st ans = new target_st();
            ans.SetPlanId(planId);
            ans.SetTargetCity(targetCity);
            ans.SetTargetAddress(targetAddress);
            ans.SetTargetName(targetName);
            ans.SetTargetType(targetType);
            ans.SetTargetLat(targetLat);
            ans.SetTargetLng(targetLng);
            ans.SetTag(Tag);
            solve solve = new solve();
            solve.target_insert(ans);
        }
    }
    public class TmpPlan
    {
        public int planId { get; set; }
        public string planName { get; set; }
        public string startTime { get; set; }
        public string endTime { get; set; }
    }

    public class TmpTarget
    {
        public int planId { get; set; }
        public string targetName { get; set; }
        public double targetLat { get; set; }
        public double targetLng { get; set; }
        public string targetAddress {  get; set; }
        public string targetType { get; set; }

    }
}
